<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="/inc/taglibs.jsp"%>
<script type="text/javascript">
function processBack(response){
	alertMsg.correct(response.message);
	$.pdialog.closeCurrent();
	navTab.reload("${path }/admin/insuranceInfo", "");
}
function uploadifySuccess(file, data, response) {
	var responseJson = $.parseJSON(data);
	if (typeof (responseJson.result) == "undefined") {
		alert("上传失败");
	} else {
		var pic = responseJson.result.imgPaths[0].original;
		pic = pic.replace(/#/, "g");
		$("#imageUrl").val(pic);
		$("#previewPic")
				.html(
						"<img id=\"coverImagePerview\" src='" + pic + "' width=\"100\" height=\"100\"/>");
	}
}

function uploadifyQueueComplete() {

}
</script>

<c:choose>
	<c:when test="${model.id==0 || empty model.id}">
		<c:set var="action" value="${path }/admin/insuranceInfo/add"></c:set>
	</c:when>
	<c:otherwise>
		<c:set var="action" value="${path }/admin/insuranceInfo/update"></c:set>
	</c:otherwise>
</c:choose>

<div class="pageContent">
	<form method="post" action="${action }" class="pageForm required-validate" onsubmit="return validateSubmit(this, processBack);">
		<input name="id" type="hidden" size="75" value="${model.id}"/>
		<div class="pageFormContent" layoutH="57" align="left">	
			<p class="nowrap">
            	<label>保险名称：</label>
				<input name="name" type="text" value="${model.name }" class="required" />
            </p>
            <p class="nowrap">  
				<label>保险简介：</label>
				<div class="unit">
					<textarea id="description" name="description" rows="5" style="width:100%; border: 1px">${model.description }</textarea>
				</div>
			</p>
			<p class="nowrap">
            	<label>保险公司：</label>
				<select id="companyId" name="companyId" onchange="setCompany(this)">
                   	<option value="">请选择</option>
                    <c:forEach var="item" items="${insuranceCompanyMap}">   
						<option value="${item.key }">${item.value}</option>
					</c:forEach> 
                </select>
                <script type="text/javascript">
                	//因为保险公司不确定有多少家，暂时写死在这里 by wushubin
                	$("#companyId").val("${model.companyId}");
                </script>
                <input id="company" name="company" value="${model.company }" type="hidden" />
            </p>
            <p class="nowrap">
            	<label>有效期：</label>
				<input id="validDate" name="validDate" type="text" value="${model.validDate }"  placeholder="填数字"  class="required"/>
            </p>
            <p class="nowrap">
				<label>有效期单位：</label>
				<select id="validDateUnit" name="validDateUnit">
					<c:forEach var="item" items="${insuranceValidDateUnitMap}">   
						<option value="${item.key }" ${model.validDateUnit == item.key ? 'selected=selected' : ''}>${item.value}</option>
					</c:forEach> 
				</select>
				<script type="text/javascript">
                	$("#validDateUnit").val("${model.validDateUnit}");
                </script>
			</p>
			<p class="nowrap">
            	<label>保险产品编号：</label>
				<input name="productId" type="text" value="${model.productId }" class="required"  />
            </p>
            <p class="nowrap">
            	<label>保险渠道：</label>
				<input name="channelId" type="text" value="${model.channelId }" class="required"  />
            </p>
            
            <p class="nowrap">
				<label>保险类型：</label>
				<select id="type" name="type">
					<option value="0">--请选择--</option> 
					<c:forEach var="item" items="${insuranceTypeMap}">
						<option value="${item.key }" ${model.type == item.key ? 'selected=selected' : ''}>${item.value}</option>
					</c:forEach> 
				</select>
				<script type="text/javascript">
                	$("#type").val("${model.type}");
                </script>
			</p>
			<p class="nowrap">
				<input id="testFileInput" type="file" name="image"
						uploaderOption="{
							swf:'${path }/js/dwz-ria-1.5.0/uploadify/scripts/uploadify.swf',
							uploader:'${path }/admin/images/upload.json',
							formData:{},
							buttonText:'请选择文件',
							fileSizeLimit:'200KB',
							fileTypeDesc:'*.jpg;*.jpeg;*.gif;*.png;',
							fileTypeExts:'*.jpg;*.jpeg;*.gif;*.png;',
							auto:true,
							multi:true,
							onUploadSuccess:uploadifySuccess,
							onQueueComplete:uploadifyQueueComplete
						}" />
				<input type="hidden" readonly="readonly" name="imageUrl" id="imageUrl"
					value="${model.imageUrl }" />
				<div id="previewPic">
					<c:if test="${not empty model.imageUrl }">
						<img id="coverImagePerview" src="${model.imageUrl}" width="100"
							height="100" />
					</c:if>
				</div>
			</p>
		</div>
		<div class="formBar">
			<ul>
			<li><div class="buttonActive"><div class="buttonContent"><button type="submit">保存</button></div></div></li>
				<li>
					<div class="button"><div class="buttonContent"><button type="button" class="close">返回</button></div></div>
				</li>
			</ul>
		</div>
	</form>
	<script type="text/javascript">  
        $(document).ready(function(){  
            //初始化xhEditor编辑器插件  
            $('#description').xheditor({  
            	tools:'mini',  
                skin:'default',  
                upMultiple:true,  
                upImgUrl: "/admin/images/xh/upload.json",  
                upImgExt: "jpg,jpeg,gif,bmp,png",  
                onUpload:insertUpload1,  
                html5Upload:false  
            });  
            //xbhEditor编辑器图片上传回调函数  
            function insertUpload1(msg) {  
                var _msg = msg.toString(); 
                $("#description").append(_msg);  
            }
            
            $("#validDate").blur(function(){
            	var _this = this;
               	if(!checkInterger(_this)) {
            	   	$(_this).val(1);
            	   	$(_this).focus();
                   	return;
               	}
    		});
            function checkInterger(obj) {
    			var _value = $(obj).val();
    			if(_value == "")
    				return true;
    			var re0 = /^[0-9]*[1-9][0-9]*$/;
    			if(_value.match(re0) == null) {
    				alert("只能输入整数！");
    				return false;
    			}
    			return true;
    		}
            
        });  
        
        function setCompany(_this){
        	var text = $("#companyId").find("option:selected").text();
        	$("#company").val(text);
        }
        
        function validateSubmit(_this,processBack){
        	if($("#imageUrl").val() == ""){
        		alert("请上传图片");
        		return false;
        	}
        	else if($("#companyId").val() == 0){
        		alert("请选择公司");
        		return false;
        	}
        	else if($("#type").val() == 0){
        		alert("请选择保险类型");
        		return false;
        	}
        	else if($("validDate").val() == ""){
        		alert("请选择填写有效期");
        		return false;
        	}
        	return validateCallback(_this, processBack)
        }
		
		
		
	</script>
</div>
